package tt.dz.biz.dao.user;

import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.vo.UserBaseVO;
import tt.dz.vo.UserServiceVO;

@Repository("userServiceDAO")
public class UserServiceDAO
  extends BaseDAO
{
  private static final String QUERY_HAS_USERSERVICE = "select count(1) from ms_user_service where user_id = :userId and service_no = :serviceNo ";
  private static final String DELETE_USERSERVICE_BY_USER = "update ms_user_service set is_valid = 0 where user_id = :userId";
  private static final String insert_userservice = "insert into ms_user_service(user_id,service_no,created_time,created_by,updated_time,updated_by,is_valid)  values(:userId,:serviceNo,:createdTime,:createdBy,:updatedTime,:updatedBy,:isValid )";
  private static final String update_userservice = "update  ms_user_service set updated_time = :updatedTime,updated_by = :updatedBy,is_valid = :isValid  where user_id = :userId and service_no = :serviceNo";
  private static final String QUERY_USERSERVICE_BY_USER = "select  a.user_id,a.service_no,a.created_time,a.created_by,a.updated_time,a.updated_by,a.is_valid  from ms_user_service as a  where a.user_id = :userId and a.is_valid = 1 ";
  
  public int deleteUserService(String userId)
  {
    UserServiceVO userServiceVO = new UserServiceVO();
    userServiceVO.setUserId(userId);
    return save("update ms_user_service set is_valid = 0 where user_id = :userId", userServiceVO);
  }
  
  public int[] batchModifyUserService(List<UserServiceVO> userServiceList)
  {
    int[] iArray = new int[userServiceList.size()];
    UserServiceVO userServiceVO = null;
    for (int i = 0; i < userServiceList.size(); i++)
    {
      userServiceVO = (UserServiceVO)userServiceList.get(i);
      int count = queryForInt("select count(1) from ms_user_service where user_id = :userId and service_no = :serviceNo ", userServiceVO);
      if ((userServiceVO.getCreatedBy() == null) && (count == 0))
      {
        userServiceVO.setCreatedBy("owenor");
        userServiceVO.setCreatedTime(new Date());
        iArray[i] = save("insert into ms_user_service(user_id,service_no,created_time,created_by,updated_time,updated_by,is_valid)  values(:userId,:serviceNo,:createdTime,:createdBy,:updatedTime,:updatedBy,:isValid )", userServiceVO);
      }
      else
      {
        userServiceVO.setUpdatedBy("owenor1");
        userServiceVO.setUpdatedTime(new Date());
        iArray[i] = save("update  ms_user_service set updated_time = :updatedTime,updated_by = :updatedBy,is_valid = :isValid  where user_id = :userId and service_no = :serviceNo", userServiceVO);
      }
    }
    return iArray;
  }
  
  public List<UserServiceVO> queryUserServiceByUser(UserBaseVO userBaseVO)
  {
    return queryForList("select  a.user_id,a.service_no,a.created_time,a.created_by,a.updated_time,a.updated_by,a.is_valid  from ms_user_service as a  where a.user_id = :userId and a.is_valid = 1 ", userBaseVO, UserServiceVO.class);
  }
  
  public int[] batchInsertUserService(List<UserServiceVO> userServiceList)
  {
    return batchSave("insert into ms_user_service(user_id,service_no,created_time,created_by,updated_time,updated_by,is_valid)  values(:userId,:serviceNo,:createdTime,:createdBy,:updatedTime,:updatedBy,:isValid )", userServiceList);
  }
}
